package com.net.model.base.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import com.drug.lib.view.load.LoadPage;
import com.net.model.R;

import me.yokeyword.fragmentation.SupportFragment;

public abstract class BaseLoadVBFragment<T extends ViewBinding> extends SupportFragment {
    protected T mBinding;
    protected Context mContext;
    protected FrameLayout contentView;
    protected LoadPage loadPage;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.base_content_fragment, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        addContentView(view);
        initialize();
        initData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBinding = null;
    }

    private void addContentView(View view) {
        contentView = view.findViewById(R.id.content_layout);
        loadPage = view.findViewById(R.id.load_page_view);
        mBinding = getViewBinding();
        ViewGroup.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        contentView.addView(mBinding.getRoot(), layoutParams);
    }

    /**
     * 获取加载布局文件
     */
    protected abstract T getViewBinding();

    /**
     * 初始化数据
     */
    protected abstract void initialize();

    protected abstract void initData();


    /**
     * description: 隐藏软键盘
     *
     * @author: zhouhequan
     * @date: 2021/10/25 11:18
     * @Param view:
     * @return: void
     */

    protected void hideSoftInput(EditText view) {
        InputMethodManager mInputMethodManager = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
        mInputMethodManager
                .hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * description: 打开软键盘
     *
     * @author: zhouhequan
     * @date: 2021/10/25 11:19
     * @Param view:
     * @return: void
     */

    protected void showSoftInput(EditText view) {
        InputMethodManager mInputMethodManager = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
        mInputMethodManager
                .showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }

    private Handler handler;

    protected final Handler getHandler() {
        if (handler == null) {
            handler = new Handler(mContext.getMainLooper());
        }
        return handler;
    }

}
